import { type App } from 'vue'
import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw
} from 'vue-router'
import { createRouterGuards } from './router-guards'
import StaticRoutes from './static-routes'
/**
 * 创建一个可以被 Vue 应用程序使用的路由实例
 * @method createRouter(options: RouterOptions): Router
 */
//普通路由 无需验证权限
export const constantRouter: any[] = [...StaticRoutes]
const router = createRouter({
  history: createWebHashHistory(''),
  routes: constantRouter,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 })
})
// 添加路由
export function addRoutes(list: any) {
  list.forEach((item: any) => {
    router.addRoute(item as RouteRecordRaw)
  })
}

export function setupRouter(app: App) {
  // 创建路由守卫
  createRouterGuards(router)
  // 使用路由
  app.use(router)
}

// 根路由
export const RootRoute: RouteRecordRaw = {
  path: '/',
  name: '/',
  component: () => import('@/layout/index.vue'),
  redirect: '/home',
  meta: {
    isKeepAlive: true
  },
  children: []
}

export default router
